package com.market.aspect;

import com.market.bean.MarketAdmin;
import com.market.bean.MarketLog;
import com.market.bean.common.BaseRespVo;
import com.market.bean.vo.admin.AdminCreateVo;
import com.market.bean.vo.admin.AdminUpdateVo;
import com.market.mapper.MarketAdminMapper;
import com.market.service.admin.AdminLogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 操作日志的增强配置
 * @since 2022/12/29 19:29
 * @author 2731235517@qq.com 谢畅
 */
@Component
@Aspect
public class AdminLogAspect {
    @Autowired
    AdminLogService adminLogService;
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Around("@annotation(com.market.annotation.AdminLogAnnotation)")
    public Object addLog(ProceedingJoinPoint joinPoint) {
        Object target = joinPoint.getTarget();
        Object proceed = null;
        // 如果就是日志本身的service类，则直接返回
        if (target instanceof AdminLogService) {
            try {
                proceed = joinPoint.proceed();
                return proceed;
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        MarketLog marketLog = new MarketLog();
        marketLog.setStatus(true);
        marketLog.setAction(joinPoint.getSignature().getName());
        marketLog.setComment("正常");
        Date date = new Date();
        marketLog.setAddTime(date);
        marketLog.setUpdateTime(date);
        marketLog.setDeleted(false);

        if (joinPoint.getSignature().getName().equals("logout")){
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String username = (String) session.getAttribute("username");
            String ip = (String) session.getAttribute("ip");
            marketLog.setAdmin(username);
            marketLog.setIp(ip);
            marketLog.setResult(username+"退出登录");
            marketLog.setType(1);
        }

        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            marketLog.setComment("出现异常");
        } finally {
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            String username = (String) session.getAttribute("username");
            String ip = (String) session.getAttribute("ip");

            if (!joinPoint.getSignature().getName().equals("logout")){
                marketLog.setAdmin(username);
                marketLog.setIp(ip);
            }
            if (joinPoint.getSignature().getName().equals("login")){
                marketLog.setResult(username+"登录");
                marketLog.setType(1);
            }else if (joinPoint.getSignature().getName().equals("create")){
                AdminCreateVo data =  (AdminCreateVo)proceed;
                marketLog.setResult(data.getUsername()+" 被创建");
                marketLog.setType(1);
            }else if (joinPoint.getSignature().getName().equals("update")){
                AdminUpdateVo data =  (AdminUpdateVo)proceed;
                marketLog.setResult(data.getUsername()+" 被修改");
                marketLog.setType(1);
            }else if (joinPoint.getSignature().getName().equals("delete")){
                MarketAdmin data =  (MarketAdmin)proceed;
                marketLog.setResult(data.getUsername()+" 被删除");
                marketLog.setType(1);
            }else if (joinPoint.getSignature().getName().equals("refund")){
                marketLog.setResult("退款");
                marketLog.setType(2);
            }else if (joinPoint.getSignature().getName().equals("shipOrder")){
                marketLog.setResult("发货");
                marketLog.setType(2);
            }else if (joinPoint.getSignature().getName().equals("deleteOrder")){
                marketLog.setResult("删除");
                marketLog.setType(2);
            }
            adminLogService.createLog(marketLog);
        }
        return proceed;
    }
}
